package com.atguigu.day07;

import com.atguigu.bean.WaterSensor;
import com.atguigu.func.WaterSensorMapFunction;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2024/7/16
 * 该案例演示了定时器
 */
public class Flink04_Timer {
    public static void main(String[] args) throws Exception {
        //TODO 1.准备环境
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(1);
        //TODO 2.从指定的网络端口中读取数据员工数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.对流中数据进行类型转换    String->WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = socketDS.map(new WaterSensorMapFunction());
                /*.assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()
                                .withTimestampAssigner(
                                        new SerializableTimestampAssigner<WaterSensor>() {
                                            @Override
                                            public long extractTimestamp(WaterSensor ws, long recordTimestamp) {
                                                return ws.getTs();
                                            }
                                        }
                                )
                );*/

        wsDS.process(
                new ProcessFunction<WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor ws, ProcessFunction<WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                        TimerService timerService = ctx.timerService();
                        timerService.registerProcessingTimeTimer(timerService.currentProcessingTime() + 10000);
                    }

                    @Override
                    public void onTimer(long timestamp, ProcessFunction<WaterSensor, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
                        System.out.println("现在时间是" + timestamp + "定时器触发");
                    }
                }
        ).print();
        /*//TODO 4.按照传感器的id进行分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);
        //TODO 5.对分组后的数据进行处理
        SingleOutputStreamOperator<String> processDS = keyedDS.process(
                new KeyedProcessFunction<String, WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor ws, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                        //获取当前分组key
                        String currentKey = ctx.getCurrentKey();
                        System.out.println("currentKey:" + currentKey);
                        //获取当前正在处理的元素的事件时间
                        Long timestamp = ctx.timestamp();
                        System.out.println("timestamp:" + timestamp);
                        //将当前数据用侧输出流标签标记(放到侧输出流中)
                        //ctx.output();

                        //获取定时服务
                        TimerService timerService = ctx.timerService();
                        //获取当前处理时间
                        long currentProcessingTime = timerService.currentProcessingTime();
                        System.out.println("currentProcessingTime:" + currentProcessingTime);

                        //获取当前水位线
                        //long currentWatermark = timerService.currentWatermark();
                        //System.out.println("currentWatermark:" + currentWatermark);

                        //注册事件时间定时器
                        //timerService.registerEventTimeTimer(10);
                        //注册处理时间定时器
                        timerService.registerProcessingTimeTimer(currentProcessingTime + 10000);

                        //删除事件时间定时器
                        //timerService.deleteEventTimeTimer(10);
                        //删除处理时间定时器
                        //timerService.deleteProcessingTimeTimer(10000);

                    }

                    @Override
                    public void onTimer(long timestamp, KeyedProcessFunction<String, WaterSensor, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
                        //定时器被触发的时候，执行的方法
                        String currentKey = ctx.getCurrentKey();
                        System.out.println("key=" + currentKey + "现在时间是" + timestamp + "定时器触发");
                    }
                }

        );
        processDS.print();*/
        env.execute();

    }
}
